Appendix F — Assignment 1

Author

phonchi

Published

May 28, 2023

F.1 (1) Which of the following equals to False?

  1. \((1 == 3)\) or \((8 == 8)\)
  2. \((2 + 2 == 4)\) and not \((2 + 2 == 5)\) and \((2 * 2 == 2 + 2)\)
  3. \((2 + 2 == 4)\) and \((2 + 8 == 5)\) and \((2 * 2 == 2 + 2)\)
  4. \((3 < 5)\) or \((1 + 5 == 6)\)

Ans: Double click to answer the question

F.2 (2) What should ‘spam’ 3* evaluate to?

  1. ‘ssspppaaammm’
  2. ‘spam’ ‘spam’ ‘spam’
  3. ‘spamssppaamm’
  4. ‘spamspamspam’

Ans: Double click to answer the question

F.3 (3) Assume that we execute the following assignnment statment:

spam1 = input()
spam2 = int(spam1)
spam3 = spam2 + 18

which variable has different data type than others?

  1. spam1
  2. spam2
  3. spam3
  4. All of them have same data type

Ans: Double click to answer the question

F.4 (4) Which of the following is not a data type?

  1. integers
  2. dictionary
  3. book
  4. strings

Ans: Double click to answer the question

F.5 (5) What does the variable monkey contain after the following code runs?

monkey = 25
monkey + 2
monkey - 5
  1. 22
  2. 27
  3. 25
  4. None of above

Ans: Double click to answer the question

F.6 (6) Why does this expression cause an error? How can you fix it?

'I have read for ' + 20 + ' minutes already.'

Ans: Double click to answer the question

The expression causes an error because 20 is an integer, and only strings can be concatenated to other strings with the + operator. The correct way is ‘I have read for’ + str(20) + ’ minutes already.’.

F.7 (7) What is the difference between the equal to operator and the assignment operator?

Ans: Double click to answer the question

== is the equal to operator that compares two values and evaluates to a Boolean, while = is the assignment operator that stores a value in a variable.

F.8 (8) What the difference between range(10), range(0, 10) and range(0, 10, 1) in a for loop?

Ans: Double click to answer the question

They all do the same thing. The range(10) call ranges from 0 up to (but not including) 10, range(0, 10) explicitly tells the loop to start at 0, and range(0, 10, 1) explicitly tells the loop to increase the variable by 1 on each iteration.

F.9 (9) Write code that prints Hello five times if 1 is stored in spam, prints Sherry 10 times if 18 is stored in spam, and prints NSYSU if anything else is stored in spam.

# coding your answer here
if spam == 1:
    for i in range(5):
        print("Hello")
elif spam == 18:
    for i in range(10):
        print("Sherry")
else:
    print("NSYSU")

F.10 (10) Write a simple program that asks the user to enter your name and How many animes have you known?, if true the program should ask How much time you have spent on watching animes? and print Be sure to spend time studying programming!, else print You're doing great in this course! Congratulation!.

sample output

Enter your name:   
Sherry   
How many animes have you known?   
5   
How much time you have spent on watching animes?   
20   
Be sure to spend time studying programming!   

or

Enter your name:   
Sherry   
How many animes have you known?   
0   
You're doing great in this course! Congratulation!
# coding your answer here
name = ''
while not name:
    print("Enter your name: ")
    name = input()

print("How many animes have you known?")
numOfAnimes = int(input())

if numOfAnimes:
    print("How much time you have spent on watching animes?")
    time = int(input())
    if time:
        print("Be sure to spend time studying programming!")
else:
    print("You're doing great in this course! Congratulation!")